package inference.traffic;

import java.io.File;

import rddl.EvalException;
import rddl.RDDL;
import rddl.RDDL.DOMAIN;
import rddl.RDDL.INSTANCE;
import rddl.RDDL.NONFLUENTS;
import rddl.competition.Client;
import rddl.competition.Server;
import rddl.parser.parser;
import rddl.policy.Policy;
import rddl.sim.Result;
import rddl.sim.Simulator;
import rddl.viz.StateViz;
import rddl.viz.TrafficDisplay;

public class TrafficPolicyExecutor implements Runnable{

	private TrafficDisplay _viz;
	private String _instName;
	private RDDL _rddl = new RDDL();
	private Simulator _sim;
	private Policy _pi;
	private long _randSeed;
	public Result _result;
	private DOMAIN _d;
	private INSTANCE _i;
	private NONFLUENTS _n;
	private boolean display;
	

	public TrafficPolicyExecutor( int nrows, int ncols, int blockSize, int wingSize, 
			int delayMillis, Policy pi, long rand_seed, final boolean disp) throws Exception{
		
		display = disp;
		
		_randSeed = rand_seed;
		
		_instName = "traffic_mdp_"+(nrows)+"_"+(ncols);
		
//		new ProblemGenerator(nrows, ncols, wingSize, blockSize, turnProb, arrivalProb,
//				_instName, horizon, discount);
		
//		Server.StartServer(new String[]{"./", "2316", "2", "15", "0", "true"});
		
		_viz = new TrafficDisplay(delayMillis, nrows, ncols, blockSize, wingSize );
		
		_rddl.addOtherRDDL( parser.parse(new File("traffic_mdp.rddl" )) );
		
		_rddl.addOtherRDDL( parser.parse(new File( _instName + ".rddl" ) ) );
		
		_d = _rddl._tmDomainNodes.values().iterator().next();
		
		_i = _rddl._tmInstanceNodes.values().iterator().next();
		
		_n = _rddl._tmNonFluentNodes.values().iterator().next();
		
		_sim = new Simulator(_rddl, _instName);
		
		_pi = pi;
		
	}
	
	@Override
	public void run() {
		
		try {
			Client cl = new Client(this._d, this._i, this._n, _pi, "localhost", 2316, "Traffic Simulator", display ? _viz : null );
		}catch(Exception e) {
			e.printStackTrace();
		}

	}
	
	public static void main(String[] args) throws Exception {
		
		Runnable r = new TrafficPolicyExecutor(3, 3, 2, 2, 3000,  
				new AdvancePolicy(), 0, true );
		
		Thread t = new Thread(r);
		t.start();
		t.join();
		
	}
	
}
